Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

117
Vistas
Find user details by ID in nested object nodejs | javascript

How to find name using id. means iterate object. create a function const searchName =()=>{} suppose if pass 3 in function so I'd want to show .... what the name of user like this

const data = [{
    "service": [
        "BUSINESS",
        "LEGAL",
        "FINANCE",
        "ADVERTISEMENT"
    ],
    "service1": [
        { "id": 1, "name": "a" },
        { "id": 2, "name": "b" },
        { "id": 3, "name": "c" },
        { "id": 4, "name": "d" },
    ],
    "service2": [
        { "id": 5, "name": "e" },
        { "id": 6, "name": "f" },
        { "id": 7, "name": "g" },
        { "id": 8, "name": "h" },
    ],
    "service3": [
        { "id": 9, "name": "i" },
        { "id": 10, "name": "j" },
        { "id": 11, "name": "k" },
        { "id": 12, "name": "l" },
    ],
    "service4": [
        { "id": 13, "name": "m" },
        { "id": 14, "name": "n" },
        { "id": 15, "name": "o" },
        { "id": 16, "name": "p" },
    ],
}
]

suppose user pass 3 so I want to return { "id": 3, "name": "c" } like this. I'm trying to iterate this and find the name of the user by id but I didn't understand this iteration so I need your help.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

check this code.... Enter any id number

const data = [{
                    "service": [
                        "BUSINESS",
                        "LEGAL",
                        "FINANCE",
                        "ADVERTISEMENT"
                    ],
                    "service1": [
                        { "id": 1, "name": "a" },
                        { "id": 2, "name": "b" },
                        { "id": 3, "name": "c" },
                        { "id": 4, "name": "d" },
                    ],
                    "service2": [
                        { "id": 5, "name": "e" },
                        { "id": 6, "name": "f" },
                        { "id": 7, "name": "g" },
                        { "id": 8, "name": "h" },
                    ],
                    "service3": [
                        { "id": 9, "name": "i" },
                        { "id": 10, "name": "j" },
                        { "id": 11, "name": "k" },
                        { "id": 12, "name": "l" },
                    ],
                    "service4": [
                        { "id": 13, "name": "m" },
                        { "id": 14, "name": "n" },
                        { "id": 15, "name": "o" },
                        { "id": 16, "name": "p" },
                    ],
                }]
            var itemobj = ''
            const searchName =(val)=>{
                    console.log('searchname')
                    data.map((item)=>{
                        let obj = Object.keys(item)            
                        obj.map((data)=>{
                            let inrdata = item[data]
                            inrdata.map((initem)=>{                    
                                 let lastdata = initem.id===val?itemobj=initem:null  
                                  
                                
                            })

                        })
                    })
                }
                
            searchName(3)
             console.log(itemobj)

about 4 years ago · Juan Pablo Isaza Denunciar

0

function searchName(id) {
let result = null;
  for (const [key, value] of Object.entries(data)) {
    if (key === "service") continue
    result = value.filter(obj => {
        return obj.id === id
        })
    if (result) break
  }
  return result ? result[0] : null
}

I iterate through keys, I just skip "service" one since it's not revelant. Then, I filter the "serviceN" array, it will return an array of object (only one if found, empty array if not found).

If it's found, we stop iterating.

Then we return either the first (and logically only element) or null if not found

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could use a combination of flat and find to get get the user by id

function searchName(id) {
  return data
    .flatMap((item) => Object.values(item))
    .flat()
    .find((user) => user.id === id);
}

const result = searchName(3); // { id: 3, name: 'c' } | undefined
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda